$(document).ready(function(){
	$('#submit').click(function(){
		$('form').submit();
	});
	
	$('#info_serie').click(function() {
		id_tvdb =  $('input[name="id_tvdb"]:checked').val();
		if(id_tvdb != undefined){
			ajax_get_info_serie(id_tvdb);
		}
		else{
			alert('Seleccione una Serie de la Lista');
		}
	});
});

function ajax_get_info_serie(id_tvdb){
	$.ajax({
		type: "POST",
		url: base_url+lang_url+'panel/series/get_info_serie',
		data: 'tvdb_id='+id_tvdb,
		dataType: 'json',
		success: function(rsp){
			if(rsp.hasOwnProperty('error')==false){
				window.parent.$('#title').val(rsp.original_name);
//				window.parent.$('#poster').val(rsp.poster);
				window.parent.$('#date_release').val(rsp.released);
				window.parent.$('#sinopsis_spa').val(rsp.synopsis_spa);
				window.parent.$('#sinopsis_eng').val(rsp.synopsis_eng);
//				window.parent.$('#trailers').val(rsp.trailers);
				
				// CAST
				var cast = [];
				var t = 0;
				for(var i in rsp.cast){
					for(var x in rsp.cast){
						if(rsp.cast[i].id == rsp.cast[x].id){
							t = t + 1;
						}
					}
					if(t==1){
						cast.push('<label>', '<input type="checkbox" name="cast[]" value="', rsp.cast[i].id ,'" checked/>', rsp.cast[i].name, '</label>');
					}
					
					if(i==10){break;}
					t = 0;
				}
				var html_cast = cast.join('');
				window.parent.$('#div-cast').empty();
				window.parent.$('#div-cast').append(html_cast);
					
				// CATEGORIES
				window.parent.$('#cat_ser').attr('value',rsp.categories);
				
				window.parent.$("p.close a").click();
			}
			else{
				alert(rsp.error);
			}
		},
		error: function(e){
			alert('Error:' + e);
		}
	});
}